<?php

namespace App\Http\Controllers\Admin;
use App\Facades\CustomPassword;
use App\Http\Requests\Admin\PersonalManage\StoreRegRequest;
use App\Models\Common\CollOption;
use App\Models\Personal\ResumeComplain;
use App\Models\Personal\PersonalUser;
use App\Models\Personal\Resume;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Style;


class CompanyComplainController extends CommonController{
    public function __construct(){
    }

    /**
     * 简历投诉
     * @param Request $request
     * @return mixed
     */
    public function resumeComplain(Request $request){
        $input   = $request->only('keyword', 'type');
        $keyword = (!empty($request->keyword)) ? trim($request->keyword) : '';
        $type    = (!empty($request->type)) ? trim($request->type).',' : '';
        $resumeComplain = DB::table('resume_complain as t1')
            ->Join('company_basic as t2', 't1.company_uid', '=', 't2.uid')
            ->Join('resume as t3', 't1.resume_id', '=', 't3.id')
            ->select(
                't1.*',
                't2.id as id1','t2.company_name',
                't3.id as id2','t3.name','t3.mobile','t3.education','t3.resume_status'
            )
            ->where(function ($query) use($keyword){
                $query->orWhere('t2.company_name', 'like', '%'.$keyword.'%')
                ->orWhere('t3.name','like','%'.$keyword.'%');
            })
            ->where(function ($query) use($type){
                $query->orWhere('t1.type', 'like', '%'.$type.'%');
            })
            ->orderBy('t1.updated_at','desc')
            ->paginate(20);

        $collOption = CollOption::where('sign', 'resume_complain')->get();//找分类表对应投诉

        foreach($resumeComplain as $k=>$v){
            $resumeComplain[$k]->education_name = CollOption::getOptionName($v->education,'education');//最高学历

            //类型名称显示前4条
            $ids       = explode(",",$v->type);
            $type_name = '';
            $i         = 0;
            foreach($ids as $k1=>$v1){
                $i++;
                if($i<=4){
                    $type_name .= CollOption::getOptionName($v1,'resume_complain').'，';
                }
            }

            if($i<=4){
                $type_name = mb_substr(mb_substr($type_name,0,-1),0,-1);
            }else{
                $type_name = mb_substr($type_name,0,-1).'...';
            }
            $resumeComplain[$k]->type_name = $type_name;
        }
        return Style::view('admin.company_manage.resume_complain',compact('collOption', 'resumeComplain'));
    }

    /**
     * 单条下沉简历
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function downComplain(Request $request){
        //简历更新到原有基础的前5天
        $resume = Resume::where('id',$request->resume_id)->first(['updatetime']);
        Resume::where('id',$request->resume_id)->update([
            'updatetime'=>date('Y-m-d H:i:s',strtotime($resume->updatetime.'-5 day'))
        ]);
        ResumeComplain::where('id',$request->id)->delete();
        return response()->json(['status' => 0,'msg' => '下沉简历成功！']);
    }

    /**
     * 批量下沉简历
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function batchDownComplain(Request $request){
        $id = $request->input('selected_id.*');
        foreach($id as $k => $v){
            $resumeComplain = ResumeComplain::where('id',$v)->first();
            $resume = Resume::where('id',$resumeComplain->resume_id)->first(['updatetime']);
            Resume::where('id',$resumeComplain->resume_id)->update([
                    'updatetime'=>date('Y-m-d H:i:s', strtotime($resume->updatetime.'-5 day'))
            ]);
            ResumeComplain::where('id',$v)->delete();
        }
        flash('批量下沉简历成功！', 'success');
        return redirect()->back();
    }

    /**
     * 单条删除简历
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function delComplain(Request $request){
        if($request->type == 1){ //删除投诉
            Resume::where('id',$request->resume_id)->update(['resume_status'=>4]);
            ResumeComplain::where('id',$request->id)->delete();
        }else{ //删除关联简历
            $obj = ResumeComplain::where('resume_id',$request->resume_id)->get();//查相同简历记录
            foreach($obj as $k=>$v){
                ResumeComplain::where('id',$v->id)->delete();
            }
            Resume::where('id',$request->resume_id)->update(['resume_status'=>4]);
        }
        return response()->json(['status' => 0,'msg' => '简历删除成功！']);
    }

    /**
     * 批量删除投诉
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function batchDelComplain(Request $request){
        $id = $request->input('selected_id.*');
        foreach($id as $k => $v){
            $resumeComplain = ResumeComplain::where('id',$v)->first();
            $count          = ResumeComplain::where('resume_id',$resumeComplain->resume_id)->count();
            if($count==1){
                Resume::where('id',$resumeComplain->resume_id)->update(['resume_status'=>4]);
            }
            ResumeComplain::where('id',$v)->delete();
        }
        flash('批量删除简历成功！', 'success');
        return redirect()->back();
    }

    /**
     * 单条驳回投诉
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function dismissedComplain(Request $request){
        $obj = ResumeComplain::where('resume_id',$request->resume_id)->get();//查相同简历记录

        if($request->type == 1){ //驳回投诉
            Resume::where('id',$request->resume_id)->update(['resume_status'=>5]);
            ResumeComplain::where('id',$request->id)->delete(); 
        }
        else{ //驳回关联投诉
            foreach($obj as $k=>$v){
                ResumeComplain::where('id',$v->id)->delete();
            }
            Resume::where('id',$request->resume_id)->update(['resume_status'=>5]);
        }
        return response()->json(['status' => 0,'msg' => '简历驳回成功！']);
    }

    /**
     * 批量驳回投诉
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function batchDismissedComplain(Request $request){
        $id = $request->input('selected_id.*');
        foreach($id as $k => $v){
            $resumeComplain = ResumeComplain::where('id',$v)->first();
            $count          = ResumeComplain::where('resume_id',$resumeComplain->resume_id)->count();
            if($count==1){
                Resume::where('id',$resumeComplain->resume_id)->update(['resume_status'=>5]);
            }
            ResumeComplain::where('id',$v)->delete();
        }
        flash('批量驳回简历成功！', 'success');
        return redirect()->back();
    }


     /**
     * 批量跳过投诉
     * @param Request $request
     * @return \Illuminate\Http\RedirectResponse
     */
    public function passComplain(Request $request){
        $selected_id = empty($request->id)?$request->input('selected_id.*'):array($request->id);
        ResumeComplain::whereIn('id',$selected_id)->delete();
        flash('跳过投诉成功！', 'success');
        return redirect()->back();
    }

}

























